<?php

namespace app\privatearea\service;

use app\common\pay\Ali;
use app\common\pay\Weixin;

class PriOrderLib
{
    /**
     *  * 统一三方下单入口
     * @param array $map
     * @param array $payConfig
     * @return array|bool
     * @throws \Exception
     */
    public static function unifyPayOrder($map = [], $payConfig = [])
    {
        $trade_type = (int)$map['trade_type'];
        $pay_des = $map['pay_des'];
        $total_amount = $map['total_amount'];
        $order_id = $map['order_id'];
        $openid = $map['openid'];
        $time = time();
        $callback_url = $map['callback_url'];
        $arr = [
            'status' => false,
            'data' => [],
        ];
        //        支付订单
        switch (true)
        {
            // 支付宝手机网页支付 支付类型为 WAP
            case $trade_type === 1:
                // 以元为单位的
                $AliPay = new Ali();
//                $payForm = $AliPay->pay($pay_des, $order_id, $total_amount, 'WAP', $order_type === 1 ? $callback_url : urlencode(urldecode($callback_url) . $order_id), $goods_type);
                //注意：一定不要urlencode，否则 用户点击完成，跳转地址为“https://mclient.alipay.com/h5/”+$callback_url（这尼玛上哪说理去）
                $payForm = $AliPay->payV2($pay_des, $order_id, $total_amount, 'WAP', $callback_url, $payConfig);
                $arr = [
                    'status' => true,
                    'data' => [
                        'par_form' => $payForm,
                        'order_id' => $order_id,
                    ],
                ];
                break;
            // 微信支付 2=>微信H5支付 3=>微信JSAPI支付
            case in_array($trade_type, [2, 3], true):
//                $total_amount = sprintf('%u', $total_amount * 100);// 以分为单位的
                // 修复小数点精度丢失问题
                $total_amount = sprintf('%u', bcmul($total_amount, 100));// 以分为单位的
                $WxPay = new Weixin();
                $payType = $trade_type === 2 ? 'H5' : 'JSAPI';
                // 获取微信配置信息
                $data = $WxPay->payV2($pay_des, $order_id, $total_amount, $payType, $payConfig, $openid);
                // 下单成功
                if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS')
                {
                    switch ($trade_type)
                    {
                        case 2: // H5支付返回
                            $sign = [
                                'mweb_url' => $data['mweb_url'] . '&redirect_url=' . urlencode($callback_url) . $order_id,
                                'order_id' => $order_id,
                            ];

                            $arr = [
                                'status' => true,
                                'data' => $sign,
                            ];
                            break;
                        case 3: // JSAPI支付
                            $sign = [
                                'appId' => $payConfig['appid'],
                                'timeStamp' => $time,
                                'nonceStr' => $WxPay->getRandChar(32),
                                'package' => 'prepay_id=' . $data['prepay_id'],
                                'signType' => 'MD5',
                            ];
                            $sign['paySign'] = $WxPay->getSignOfJsApi($sign, $payConfig);

                            $sign['order_id'] = $order_id;
                            $arr = [
                                'status' => true,
                                'data' => $sign,
                            ];
                            break;
                        default:
                            break;
                    }
                } else
                {
                    // $ret = [
                    //     'return_code' => $data['return_code'],
                    //     'return_msg' => $data['return_msg'],
                    // ];
                    $arr = [
                        'status' => false,
                        'data' => $data,
                    ];
                }
                break;
            default :
                return false;
        }

        return $arr;
    }
}